﻿using System;
using System.IO;
using System.Windows.Forms;

namespace e3winform
{
    public partial class Form1 : Form
    {
        private string file1Path = "";
        private string file2Path = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                file1Path = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog2 = new OpenFileDialog();

            openFileDialog2.InitialDirectory = "c:\\";
            openFileDialog2.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog2.FilterIndex = 2;
            openFileDialog2.RestoreDirectory = true;

            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                file2Path = openFileDialog2.FileName;
            }
        }

        private void MergeFiles(string file1Path, string file2Path)
        {
            string mergedFilePath = @"D:\cpp\作业5\e3winform\TextFile1.txt";

            using (StreamWriter sw = new StreamWriter(mergedFilePath))
            {
                string file1Content = File.ReadAllText(file1Path);
                string file2Content = File.ReadAllText(file2Path);

                sw.Write(file1Content);
                sw.Write(file2Content);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(file1Path) && !string.IsNullOrEmpty(file2Path))
            {
                MergeFiles(file1Path, file2Path);
                MessageBox.Show("Files merged successfully!");
            }
            else
            {
                MessageBox.Show("Please select both files first.");
            }
        }
    }
}
